package com.example.exp_3

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.exp_3.R
import com.example.exp_3.ui.theme.Exp_3Theme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            Exp_3Theme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    DiceImageBrowser(modifier = Modifier.padding(innerPadding))
                }
            }
        }
    }
}

@Composable
fun DiceImageBrowser(modifier: Modifier = Modifier) {
    // 定义图片资源数组
    val diceImages = remember {
        listOf(
            R.drawable.dice_1,
            R.drawable.dice_2,
            R.drawable.dice_3
        )
    }

    // 定义每张图片的名称
    val diceNames = remember {
        listOf(
            "图 1",
            "图 2",
            "图 3"
        )
    }

    // 跟踪当前显示的图片索引
    var currentIndex by remember { mutableStateOf(0) }

    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // 标题
        Text(
            text = "图片浏览器",
            fontSize = 24.sp,
            fontWeight = FontWeight.Bold,
            modifier = Modifier.padding(bottom = 16.dp)
        )

        // 图片卡片
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp)
        ) {
            Column(
                modifier = Modifier.padding(16.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                // 显示当前图片
                Image(
                    painter = painterResource(id = diceImages[currentIndex]),
                    contentDescription = diceNames[currentIndex],
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(300.dp)
                )

                Spacer(modifier = Modifier.height(16.dp))

                // 显示图片名称
                Text(
                    text = diceNames[currentIndex],
                    fontSize = 20.sp,
                    fontWeight = FontWeight.Medium,
                    color = MaterialTheme.colorScheme.primary
                )
            }
        }

        Spacer(modifier = Modifier.height(24.dp))

        // 上一张、下一张按钮行
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceEvenly
        ) {
            Button(
                onClick = {
                    // 计算上一张索引，支持循环
                    currentIndex = if (currentIndex == 0) {
                        diceImages.size - 1
                    } else {
                        currentIndex - 1
                    }
                }
            ) {
                Text("上一张")
            }

            Button(
                onClick = {
                    // 计算下一张索引，支持循环
                    currentIndex = (currentIndex + 1) % diceImages.size
                }
            ) {
                Text("下一张")
            }
        }

        // 添加当前图片位置指示器
        Spacer(modifier = Modifier.height(16.dp))
        Text(
            text = "${currentIndex + 1} / ${diceImages.size}",
            fontSize = 16.sp,
            modifier = Modifier.padding(8.dp)
        )
    }
}

@Preview(showBackground = true)
@Composable
fun DiceImageBrowserPreview() {
    Exp_3Theme {
        DiceImageBrowser()
    }
}